package com.sqlcheck.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.sqlcheck.common.enums.DatabaseType;
import com.sqlcheck.util.JsonUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 大模型优化记录实体
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "llm_optimization_records")
public class LLMOptimizationRecord {

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @TableField("task_id")
    private Long taskId;

    @TableField("original_sql")
    private String originalSql;

    @TableField("optimized_sql")
    private String optimizedSql;

    @TableField("source_db_type")
    private DatabaseType sourceDbType;

    @TableField("target_db_type")
    private DatabaseType targetDbType;

    @TableField("llm_provider")
    private String llmProvider;

    @TableField("llm_model")
    private String llmModel;

    @TableField("llm_version")
    private String llmVersion;

    @TableField("prompt_content")
    private String promptContent;

    @TableField("llm_response")
    private String llmResponse;

    @TableField("optimization_summary")
    private String optimizationSummary;

    @TableField("diff_highlights")
    private String diffHighlightsJson; // JSON格式存储

    @TableField("detected_issues")
    private String detectedIssuesJson; // JSON格式存储

    @TableField("status")
    private OptimizationStatus status;

    @TableField("processing_time")
    private Integer processingTime;

    @TableField("confidence_score")
    private BigDecimal confidenceScore;

    @TableField("error_message")
    private String errorMessage;

    @TableField("user_id")
    private Long userId;

    @TableField("request_ip")
    private String requestIp;

    @TableField("user_agent")
    private String userAgent;

    @TableField(value = "created_at", fill = FieldFill.INSERT)
    private LocalDateTime createdAt;

    @TableField(value = "updated_at", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedAt;

    @TableLogic
    @TableField("deleted")
    private Boolean deleted;

    // Helper methods for JSON fields
    public List<OptimizationHighlight> getDiffHighlights() {
        return JsonUtils.jsonToList(this.diffHighlightsJson, OptimizationHighlight.class);
    }

    public LLMOptimizationRecord setDiffHighlights(List<OptimizationHighlight> highlights) {
        this.diffHighlightsJson = JsonUtils.listToJson(highlights);
        return this;
    }

    public List<CompatibilityIssue> getDetectedIssues() {
        return JsonUtils.jsonToList(this.detectedIssuesJson, CompatibilityIssue.class);
    }

    public LLMOptimizationRecord setDetectedIssues(List<CompatibilityIssue> issues) {
        this.detectedIssuesJson = JsonUtils.listToJson(issues);
        return this;
    }

    /**
     * 优化状态枚举
     */
    public enum OptimizationStatus {
        SUCCESS,           // 成功
        FAILED,           // 失败
        TIMEOUT,          // 超时
        PARSING_ERROR     // 解析错误
    }

    /**
     * 优化高亮信息
     */
    @Data
    public static class OptimizationHighlight {
        private int startLine;
        private int endLine;
        private int startColumn;
        private int endColumn;
        private String type; // 'added', 'removed', 'modified'
        private String description;
        private String originalText;
        private String optimizedText;
        private String reason; // 修改原因
    }

    /**
     * 兼容性问题
     */
    @Data
    public static class CompatibilityIssue {
        private String type;
        private String severity;
        private String description;
        private String suggestion;
        private Integer lineNumber;
        private String originalText;
        private String optimizedText;
        private String ruleCode;
    }
}